<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/2/3
 * Time: 17:57
 */
namespace app\home\controller;
use think\Db;
use think\Session;

class CenterController extends CommonController{
    /**
     * userCenter 展示个人中心页面
     * @return HTML个人中心页面
     */
    public function userCenter(){
        $res = session('studentSession');
        if(is_null($res)){
            return $this->redirect('Login/showLogin');
        }else{
//            var_dump($res);die;

//            $course = Db::table('ew_subject')->where('s_id',$res['stu_course'])->value('s_name');
            $course = Db::table('ew_subject')->select();
            $this->assign('res',$res);
            $this->assign('course',$course);
            return $this->fetch('index/center_1');
        }
    }

    /**
     * personalInformationUpdate 展示个人信息修改页面
     * @return 返回HTML页面
     */
    public function personalInformation(){
        return $this->fetch('index/center');
    }

    /**
     * personalInformationUpdate  修改个人信息
     * @return 返回修改数据库修改结果
     */
    public function personalInformationUpdate(){
        extract(input());
        $res = Db::table('ew_student')
            ->where('stu_id',session('studentSession')['stu_id'])
            ->update(['stu_nickname'=>$stu_nickname,'stu_qq'=>$stu_qq,'stu_zoom'=>$stu_zoom,'stu_updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            $arr = Db::table('ew_student')
                ->where('stu_id',session('studentSession')['stu_id'])
                ->find();
            session('studentSession',$arr);
            $resultData['status'] = 1;
            $resultData['msg'] = "修改成功";
            return json_encode($resultData);
        }else{
            $resultData['status'] = 0;
            $resultData['msg'] = "修改失败";
            return json_encode($resultData);
        }
    }

    /**
     *personalCourseUpdate 修改考试信息
     * @return 返回修改数据库结果
     */
    public function personalCourseUpdate(){
        extract(input());
        $res = Db::table('ew_student')
            ->where('stu_id',session('studentSession')['stu_id'])
            ->update(['stu_course'=>$stu_course,'stu_testtime'=>$stu_testtime,'stu_score'=>$stu_score,'stu_ziliaostatus'=>$stu_ziliaostatus,'stu_updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            $arr = Db::table('ew_student')
                ->where('stu_id',session('studentSession')['stu_id'])
                ->find();
            session('studentSession',$arr);
            $resultData['status'] = 1;
            $resultData['msg'] = "修改成功";
            return json_encode($resultData);
        }else{
            $resultData['status'] = 0;
            $resultData['msg'] = "修改失败";
            return json_encode($resultData);
        }
    }


}
